<?php

ini_set('session.use_only_cookies', 1);

date_default_timezone_set('Europe/Moscow');

define('SMARTY_DIR','./smarty/');
require_once SMARTY_DIR . 'Smarty.class.php';
$smarty = new Smarty;
$smarty->compile_check = true;
$smarty->debugging = false;
$smarty->addPluginsDir('./cf/components/');
$smarty->addPluginsDir('./components/');


$templateVars = array();

function assignTemplateVars($varName1) 
{
   global $templateVars;
   $args = func_get_args();
   $templateVars = array_merge($templateVars, $args);
}

function display($templateName) 
{
   global $smarty, $templateVars;
   foreach ($templateVars as $varName) {
      $smarty->assignByRef( $varName, $GLOBALS[$varName] );
   }
   $smarty->assign('templateName',$templateName);

   $smarty->display($templateName . '.tpl');
}


require_once 'cf/init.inc';
require_once 'cf/page.inc';
require_once 'cf/shop.inc';
require_once 'cf/gallery.inc';
require_once 'cf/image.inc';
require_once 'cf/session.inc';
require_once 'cf/user.inc';


$pagesMenuOb = new ChildPages(ROOT_PAGE);
$pagesMenu = $pagesMenuOb->toArray();
assignTemplateVars('pagesMenu');


$allGroupsOb = new DescendantGroups(Group::rootId);
$allGroups = $allGroupsOb->toArray();
assignTemplateVars('allGroups');

$contacts = new Block('contacts');
assignTemplateVars('contacts');

$mainTop = new Block('main_top');
assignTemplateVars('mainTop');


$path = array();
assignTemplateVars('path');

function addGroupToPath($groupId)
{
   global $path;
   $ancestors = new AncestorGroups($groupId);
   foreach ($ancestors as $ancestor) {
      $path[] = array($ancestor->name(), 'group.php?groupId='.$ancestor->id());
   }
}



define('ARTICLES_PAGE_ID', 7);
define('FAQ_PAGE_ID', 8);



$articleAnnounces = query2arrays("
	SELECT articles.id as id, articles.title as title, articles_on_main.title as text
	FROM articles_on_main
	INNER JOIN articles ON articles_on_main.article=articles.id
	ORDER BY articles_on_main.sort_order
");
assignTemplateVars('articleAnnounces');

$randomPhoto = null;

function getImageNumberInAlbumByID($albumId, $imageId)
{
	$imgsInAlbum = new GalleryImages($albumId);
	$cnt = 1;

	foreach ($imgsInAlbum as $img)
	{
		if ($img->id() == $imageId)
		{
			return $cnt;
		}
		++$cnt;
	}

	return 1;
}

if (query2var("SELECT EXISTS(SELECT * FROM cf_gallery)"))
{
	$randomPhoto = array();
	$randImage = new RandomGalleryImage();
	$randomPhoto['image'] = new Thumbnail($randImage->image(), 250, 150);
	$randomPhoto['albumId'] = $randImage->albumId();
	$randomPhoto['numberInAlbum'] = getImageNumberInAlbumByID($randImage->albumId(), $randImage->id());
	$randomPhoto['name'] = $randImage->name();
	$randomPhoto['descr'] = $randImage->descr();
}

assignTemplateVars('randomPhoto');



define('VIDEO_ID',1);

$randomVideo = Element::getRandom(VIDEO_ID);
assignTemplateVars('randomVideo');

$search = '';
$smarty->assignByRef('search', $search);


$basket = new Basket;
$smarty->assign('in_basket_now', array('count'=>$basket->totalProducts(), 'sum'=>$basket->totalSum()));

session::open();

?>